package jk.fox.config.transport.netty;

import jk.fox.config.RpcConfiguration;
import jk.fox.config.transport.TransportConfiguration;
import jk.fox.config.transport.TransportConfigurationBuilder;

/**
 * @author cuichao
 * @Description: NettyConfigurationBuilder
 * @Date: create in 2020/5/23 13:58
 */
public class NettyConfigurationBuilder extends TransportConfigurationBuilder {


    private NettyConfiguration nettyConfig;

    public NettyConfigurationBuilder(RpcConfiguration.RpcConfigBuilder rpcConfig) {
        super(rpcConfig);
        this.nettyConfig = new NettyConfiguration();
    }

    @Override
    public TransportConfiguration build() {
        return nettyConfig;
    }

    public NettyConfigurationBuilder serverPort(int port){
        nettyConfig.setServerPort(port);
        return this;
    }

    public NettyConfigurationBuilder serverWorkGroup(int size){
        nettyConfig.setServerWorkGroup(size);
        return this;
    }

    public NettyConfigurationBuilder serverTcpBacklog(int num){
        nettyConfig.setServerTcpBacklog(num);
        return this;
    }


    public NettyConfigurationBuilder keepAlive(boolean keepAlive){
        nettyConfig.setKeepAlive(keepAlive);
        return this;
    }


    public NettyConfigurationBuilder serverHeartBeat(int second){
        if(second > 0){
            nettyConfig.setEnableServerHeartBeat(true);
            nettyConfig.setReaderIdleTimeSeconds(second);
            nettyConfig.setWriterIdleTimeSeconds(second);
        }else{
            nettyConfig.setEnableServerHeartBeat(false);
        }
        return this;
    }


    public NettyConfigurationBuilder clientHeartBeat(int second){
        if(second > 0){
            nettyConfig.setEnableClientHeartBeat(true);
            nettyConfig.setReaderIdleTimeSeconds(second);
            nettyConfig.setWriterIdleTimeSeconds(second);
        }else{
            nettyConfig.setEnableClientHeartBeat(false);
        }
        return this;
    }

    public NettyConfigurationBuilder disableNagle(boolean disable){
        nettyConfig.setDisableNagle(disable);
        return this;
    }


}
